package mysql

import "fmt"

func Select1() {
	var student Student
	// select * from students order by id limit 1
	// 指针   未指定主键的话 按照表的第一个字段进行排序
	DB.First(&student)
	fmt.Printf("student.ID: %v\n", student.ID)
	student = Student{}
	// select * from students limit 1
	DB.Take(&student)
	fmt.Printf("student.ID: %v\n", student.ID)
	student = Student{}
	// select * from students order by id desc limit 1
	DB.Last(&student)
	fmt.Printf("student.ID: %v\n", student.ID)
	student = Student{}

}

func Select2() {
	student := map[string]interface{}{}
	// 有效  因为指定了Model
	DB.Model(&User{}).First(&student)

	student = map[string]interface{}{}
	// 无效
	DB.Table("students").First(&student)

	student = map[string]interface{}{}
	// 配合take有效
	DB.Table("students").Take(&student)

}

func Select3() {
	// 根据主键查询  主键为数字类型的
	var student = Student{}
	DB.First(&student, 10)
	fmt.Printf("student.Name: %v\n", student.Name)
	student = Student{}
	DB.First(&student, "10")
	fmt.Printf("student.Name: %v\n", student.Name)
	var students []Student
	DB.Find(&students, []int{1, 2, 3})
	for _, v := range students {
		fmt.Printf("v.ID: %v\n", v.ID)
	}

	// 主键为字符串的话
	// DB.First(&students, "id = ?", "11ewqewr")

}

func Select4() {
	var students []Student
	ret := DB.Find(&students)
	fmt.Printf("ret.RowsAffected: %v\n", ret.RowsAffected)
}

func Select5() {
	var student Student
	DB.Where("name=?", "zhoujl").First(&student)
	// 还有很多高级查询见文档https://gorm.io/zh_CN/docs/query.html
}
